


import 'package:zfth/pages/home/homecount/HomeCountLogic.dart';
import 'package:zfth/pages/home/homecount/HomeCountPage.dart';
import 'package:zfth/pages/home/homegrid/HomeGridLogic.dart';
import 'package:zfth/pages/home/homegrid/HomeGridPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:convert' as convert;
import 'dart:core';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../../components/form/LFormPage.dart';
import '../../utils/SPutil.dart';
import '../../utils/Settings.dart';
import '../../utils/Utils.dart';
import '../../utils/WebSocketUtil.dart';
import '../../utils/eventbus/EventBus.dart';
import '../../utils/eventbus/EventBusMethod.dart';
import 'dart:io' show Directory, Platform;
import '../../utils/MyUpdateDialog.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../api/SysConfig.dart';
import 'package:get/get.dart';

class HomeIndexPage extends StatefulWidget{
  const HomeIndexPage({super.key});

  @override
  State<HomeIndexPage> createState() =>HomeIndexPageState();

}

class HomeIndexPageState extends State<HomeIndexPage> with AutomaticKeepAliveClientMixin{

  @override
  void initState() {

    //初始化webSocket
    initWebSocket();

    super.initState();

  }

  @override
  void dispose() {
    Get.delete<HomeGridLogic>();
    Get.delete<HomeCountLogic>();
    super.dispose();
  }

  @override
  bool get wantKeepAlive => true;

  final HomeGridLogic homeGridLogic = Get.put(HomeGridLogic());

  final HomeCountLogic homeCountLogic = Get.put(HomeCountLogic());

  ///开始自定义业务

  initWebSocket(){
    dynamic token = SPutil.getLocalStorage(Settings.localStorageKeys["token"]);
    dynamic empNo = SPutil.getLocalStorage(Settings.localStorageKeys["empNo"]);
    if(token==null || empNo==null){
      return;
    }
    WebSocketUtil.getInstance().initWebSocket(onOpen: () {
      WebSocketUtil.getInstance().authentication(token, empNo);
    }, onMessage: (data) {
      //发布事件
      bus.emit(EventBusMethod.methods["wbSocket事件"],convert.jsonDecode(data));
    }, onError: (e) {
      print(e);
    });
  }

  ///版本比对，弹出更新
  checkVersion(){
    Future.delayed(const Duration(milliseconds: 1000), ()async {
      //获取当前本班
      PackageInfo packageInfo  = await Utils.getPackageInfo();
      String currentVersion = packageInfo.version;
      if(Platform.isAndroid){
        //如果是安卓，查询安卓最新版本
        dynamic androidVersionRes = await SysConfigApi.getScSysConfigByCode(Settings.sysConfig["安卓APP版本号参数"]);
        if(androidVersionRes["success"]){
          String newVersion = androidVersionRes["result"];
          if(newVersion!=currentVersion){
            if(mounted){
              MyUpdateDialog.defaultStyle(context,"更新提醒","发现新版本，请更新！");
            }
          }
        }
      }else{

      }


    });

  }





  ///自定义业务结束


  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        backgroundColor: Settings.backgroundColor,
        appBar: AppBar(
          title: const Text("环境监测"),
          centerTitle: true,
          elevation: 0,
        ),
        body: SingleChildScrollView(
          child: Wrap(
            children: [
              Column(
                children: const [
                  HomeGridPage(),
                  HomeCountPage()
                ],
              )
            ],
          ),
        ));
  }

}